#include <typescommon.h>
#include <genrand.h>
#include <pthread.h>
#include "listopt.h"

// 自定义数据测试
#pragma pack(1)
struct userDataInformSample
{
    uint32_t userid;
    char username[32];
};
enum listUsrValueEnum
{
    listUsrValueSample = 7,
};
// list user node sample pointer.
#define listUsrNdSplPtr(ncp) listUsrNdPtr(ncp, struct userDataInformSample)
int userSampleInputList(struct list_node_root *listroot, uint32_t idset, int backflag)
{
    struct listUsrNdStct *tmpnode = listUsrMlcSpc(listUsrValueSample, sizeof(struct userDataInformSample));
    if(!tmpnode)
    {
        logerror("");
        return -1;
    }
    listUsrNdSplPtr(tmpnode)->userid = idset;
    snprintf(listUsrNdSplPtr(tmpnode)->username, sizeof(listUsrNdSplPtr(tmpnode)->username), "lxin-%u", idset);
    return listUsrPushNd(listroot, tmpnode, backflag);
}
#pragma pack()
int main(int argc, char **argv)
{
    static struct list_node_root checkroot;
    listRootInit(&checkroot);
    struct listUsrNdStct *tmpuser;
    {//测试int型基础数据类型的队列。
        for (int i = 0; i < 10; i++)
        {
            listUsrPsbkU64(&checkroot, i);
            listUsrPsfrU64(&checkroot, i + 10000);
            tmpuser = listUsrPopBack(&checkroot);
            if(tmpuser)
            {
                logdebug("get back;type=%d,value=%d", tmpuser->valtype, tmpuser->valint);
                free(tmpuser);
            }
        }
        
        listRootFree(&checkroot);//释放失败。

        while(tmpuser = listUsrPopFront(&checkroot), tmpuser)
        {
            logdebug("get remain front;type=%d,value=%d", tmpuser->valtype, tmpuser->valint);
            free(tmpuser);
        }
        logdebug("");
        listRootFree(&checkroot);
    }

    {//测试自定义结构体的队列
        for (int i = 0; i < 10; i++)
        {        
            userSampleInputList(&checkroot, i, 1);
            userSampleInputList(&checkroot, i + 10000, 0);
            tmpuser = listUsrPopNd(&checkroot, 1);
            if (tmpuser)
            {
                logdebug("get back, type=%d, size=%u, id=%u, name=%s", tmpuser->valtype, tmpuser->valsize,
                        listUsrNdSplPtr(tmpuser)->userid, listUsrNdSplPtr(tmpuser)->username);
                free(tmpuser);
            }
            userSampleInputList(&checkroot, i + 20000, 1);
        }

        listRootFree(&checkroot);

        while (tmpuser = listUsrPopNd(&checkroot, 0), tmpuser)
        {
            logdebug("get remain front, type=%d, size=%u, id=%u, name=%s", tmpuser->valtype, tmpuser->valsize,
                    listUsrNdSplPtr(tmpuser)->userid, listUsrNdSplPtr(tmpuser)->username);
            free(tmpuser);
        }
        logdebug("");
        listRootFree(&checkroot);
    }
    return 1;
}
